package com.team.RunsFast_Api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.RunsFast_Api.config.RedisKeyConfig;
import com.team.RunsFast_Api.config.SystemConfig;
import com.team.RunsFast_Api.dao.UserDao;
import com.team.RunsFast_Api.dao.UserLevelDao;
import com.team.RunsFast_Api.dto.UserDto;
import com.team.RunsFast_Api.dto.UserLoginCodeDto;
import com.team.RunsFast_Api.dto.UserLoginDto;
import com.team.RunsFast_Api.service.intf.UserService;
import com.team.RunsFast_Api.util.JedisUtil;
import com.team.RunsFast_Api.util.PassUtils;
import com.team.runsfast.entity.User;
import com.team.runsfast.entity.UserLevel;
import com.team.runsfast.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl implements UserService {
    private final UserDao dao;
    private final UserLevelDao levelDao;
    public UserServiceImpl(UserDao dao, UserLevelDao levelDao){
        this.dao=dao;
        this.levelDao = levelDao;
    }
    @Override
    public R register(UserDto dto) {
        if(dto!=null){
            if(StringUtils.hasLength(dto.getPhone())&& StringUtils.hasLength(dto.getPassword())){
                QueryWrapper<User> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",dto.getPhone());
                User user=dao.selectOne(queryWrapper);
                if(user==null){
                    if(JedisUtil.existsSet(RedisKeyConfig.USER_ADDPHONES,dto.getPhone())){
                        user = new User(dto.getPhone(), PassUtils.aesenc(SystemConfig.PASS_KEY, dto.getPassword()));
                        if(dao.insert(user)>0){
                            levelDao.insert(new UserLevel(user.getId(), 1000L, "第一次注册，奖励积分，完成等级初始化"));
                            JedisUtil.delSet(RedisKeyConfig.USER_ADDPHONES,dto.getPhone());
                            return R.ok(null);
                        }
                    }
                }
            }
        }
        return R.fail();
    }

    @Override
    public R update(UserDto dto) {
        if(dto!=null){
            if(StringUtils.hasLength(dto.getPhone())&& StringUtils.hasLength(dto.getPassword())){
                QueryWrapper<User> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",dto.getPhone());
                User user=dao.selectOne(queryWrapper);
                if(user!=null){
                    if(JedisUtil.existsSet(RedisKeyConfig.USER_ADDPHONES,dto.getPhone())){
                        user.setPassword(PassUtils.aesenc(SystemConfig.PASS_KEY, dto.getPassword()));
                        if(dao.update(user)>0){
                            JedisUtil.delSet(RedisKeyConfig.USER_ADDPHONES,dto.getPhone());
                            return R.ok(null);
                        }
                    }
                }
            }
        }
        return R.fail();
    }

    @Override
    public R checkPhone(String phone) {
        if(StringUtils.hasLength(phone)){
            if(phone.matches("1[3-9][0-9]{9}$")){
                QueryWrapper<User>queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",phone);
                User user=dao.selectOne(queryWrapper);
                if(user==null){
                    return R.ok(null);
                }
            }
        }
        return R.fail();
    }

    @Override
    public R checkPhoney(String phone) {
        if(StringUtils.hasLength(phone)){
            if(phone.matches("1[3-9][0-9]{9}$")){
                QueryWrapper<User>queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",phone);
                User user=dao.selectOne(queryWrapper);
                if(user!=null){
                    return R.ok(null);
                }
            }
        }
        return R.fail();
    }

    @Override
    public R all() {
        return R.ok(dao.selectList(null));
    }

    @Override
    public R loginPassword(UserLoginDto dto) {
        if(dto!=null){
            if(StringUtils.hasLength(dto.getPhone())){
                QueryWrapper<User> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",dto.getPhone());
                User user=dao.selectOne(queryWrapper);
                if(user!=null){
                    if(user.getPassword().equals(PassUtils.aesenc(SystemConfig.PASS_KEY,dto.getPassword()))){
                        if(JedisUtil.exists(RedisKeyConfig.USER_PHONE+dto.getPhone())){
                            JedisUtil.addHash(RedisKeyConfig.USER_LOGIN_DROP,dto.getPhone(),"亲。你在其他设备已登录！");
                        }
                        String token=user.getId()+"";
                        JedisUtil.addString(RedisKeyConfig.USER_PHONE+dto.getPhone(),token,RedisKeyConfig.USER_PHONE_TIME);
                        return R.ok(token);
                    }
                }
            }
        }
        return R.fail();
    }
//手机验证登录
    @Override
    public R loginCode(UserLoginCodeDto dto) {
        if(dto!=null){
            if(StringUtils.hasLength(dto.getPhone())){
                QueryWrapper<User> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("phone",dto.getPhone());
                User user=dao.selectOne(queryWrapper);
                if(user!=null){
                    if(user.getFlag()==SystemConfig.USER_OK){
                        if(JedisUtil.exists(RedisKeyConfig.USER_ADDCODE+dto.getPhone())){
                            if(JedisUtil.exists(RedisKeyConfig.USER_PHONE,dto.getPhone())){
                                JedisUtil.addHash(RedisKeyConfig.USER_LOGIN_DROP,dto.getPhone(),"亲。你在其他设备已登录！");
                            }
                            String token=user.getId()+"";
                            JedisUtil.addString(RedisKeyConfig.USER_PHONE+dto.getPhone(),token,RedisKeyConfig.USER_PHONE_TIME);
                            JedisUtil.delKey(RedisKeyConfig.USER_LOGINCODE+dto.getPhone());
                            return R.ok(token);
                        }

                    }
                }
            }

        }
        return R.fail("亲，账号或密码不正确！");

    }
//退出登录
    @Override
    public R loginOut(String phone) {
        if(StringUtils.hasLength(phone)){
            if(JedisUtil.exists(RedisKeyConfig.USER_PHONE+phone)){
                JedisUtil.delKey(RedisKeyConfig.USER_PHONE+phone);
                return R.ok();
            }
        }
        return R.fail("亲，你都没有登录！");
    }

    @Override
    public R checkToken(String phone) {
        if(StringUtils.hasLength(phone)){
            if(JedisUtil.exists(RedisKeyConfig.USER_PHONE+phone)){
                JedisUtil.delHash(RedisKeyConfig.USER_LOGIN_DROP,phone);
                return R.ok();
            }
        }
        return R.fail();
    }

    //查询当前用户信息
    @Override
    public R selectOne(String token) {

        return R.ok(dao.selectOne(new QueryWrapper<User>().eq("id",Long.parseLong(token))));
    }
}
